﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class EditForm : Form
    {
        public EditForm()
        {
            InitializeComponent();
        }
        private int id;
        public EditForm(int id,string title,string author,string time)
        {
            InitializeComponent();
            this.id = id;
            this.textBox1.Text = title;
            this.textBox2.Text = author;
            this.textBox3.Text = time;
        }
        /// <summary>
        /// 确认保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            var title = this.textBox1.Text;
            var author = this.textBox2.Text;
            var time = this.textBox3.Text;
            var select = string.Format("select*from Articles where Title='{0}'", title);
            DbHelper db = new DbHelper();
            var res = DbHelper.GetDataTable(select);
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(author) && !string.IsNullOrEmpty(time))
                {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',CreateTime='{2}' where Id='{3}'", title, author, time, id);
                    var update = DbHelper.AddOrUpdateOrDelete(sql);
                    if (update == 1)
                    {
                        MessageBox.Show("更新成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }

                }
                else 
                {
                    MessageBox.Show("请填入内容进行更新");
                }

            }
            else 
            {
                if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(author) && !string.IsNullOrEmpty(time))
                {

                    var sql = string.Format("insert into Articles values('{0}','{1}','{2}')", title, author, time);
                    var dt = DbHelper.AddOrUpdateOrDelete(sql);
                    if (dt == 1)
                    {
                        MessageBox.Show("添加成功");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }

                }
                else if (res.Rows.Count > 0)
                {

                    MessageBox.Show("该书已添加");
                }
                else
                {
                    MessageBox.Show("添加内容不能为空");
                }
            }


        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
